const sharp = require("sharp");

const fs = require("fs");

const path = require("path");

const sourcePath = path.join("arena-zh");
fs.existsSync(sourcePath);
if (!sourcePath) {
  fs.mkdirSync(sourcePath, { recursive: true });
}

fs.readdirSync(sourcePath).forEach(async (file) => {
  const image = path.join(sourcePath, file);
  const localPath = path.join("arena-zh-output");
  const prepareFinish = fs.existsSync(localPath);
  if (!prepareFinish) {
    fs.mkdirSync(localPath, { recursive: true });
  }
  const output = path.join(localPath, file.replace(".png", ".jpg"));
  await sharp(image)
    .resize(200) // 设置图片宽度为 200px
    .jpeg({ quality: 80 }) // 设置图片质量为 80%
    .toBuffer()
    .then(async (data) => {
      fs.writeFileSync(output, data);
    })
    .catch((err) => console.log(err));

  console.log(output + " : done");
});

// TODO CONAN

// fs.readdirSync(path.join('cardlist')).forEach(async file => {
// 	const image = path.join('cardlist', file);
// 	// console.log(file);
// 	const output = path.join('thumbnails', file);
// 	await sharp(image)
// 		.resize(200)
// 		.jpeg({ quality: 80 })
// 		.toBuffer().then(data => {
// 			fs.writeFileSync(output, data);
// 			console.log(output + ' : done');
// 		}).catch(err => console.log(err));
// });

// TODO TIERS
// const image = path.join('tier-full.png');
// const output = path.join('tier-full.jpg');
// sharp(image)
// 	.jpeg({ quality: 80 })
// 	.toBuffer().then(data => {
// 		fs.writeFileSync(output, data);
// 		console.log(output + ' : done');
// 	}).catch(err => console.log(err));
